import http from "../../utils/http"
// pages/recommend/recommend.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[],
    comList:[],
    timeList:0,
    lastId:0
  },
  goSearch(){
   wx.navigateTo({
     url: '../search/search',
   })
  },
  getTime(){
    let timeArr = [];
    this.data.timeList.forEach(item =>{
      let minut  = parseInt(item.duration / 1000 / 60)
      let second = parseInt(item.duration % 60)
      minut  = minut  < 10 ? "0"+minut  : minut ;
      second = second < 10 ? "0"+second : second;

      let timeStr = minut + ":"+second
      timeArr.push(timeStr)
    })
    // console.log(timeArr);
    this.setData({
      timeList:timeArr
    })
    // console.log(this.data.timeList);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   http.get("https://app.vmovier.com/apiv3/index/index").then(
     res => {
      // 轮播图列表
      //  console.log(res.data.data);
      //  console.log(res.data.data.hot.list);
      // wx.showLoading({
      //   title: '正在加载...',
      // })
       this.setData({
        bannerList:res.data.data.banner.list,
        comList:res.data.data.hot.list,
        timeList:res.data.data.hot.list
       })
       this.getTime();
       
      // console.log(this.data.bannerList);
       
     }
   )
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '加载中...',
    })
    http.get(`https://app.vmovier.com/apiv3/index/getIndexPosts/lastid/${this.data.lastId}`).then(
      res => {
        
        // console.log(res.data.data.list);
        let arr = res.data.data.list;
        let newArr = this.data.comList;
        let lastid = res.data.data.lastid;
        newArr = arr.concat(newArr);
        // console.log(arr);
        this.setData({
          comList:newArr,
          lastId:lastid
        })
        
      wx.hideLoading()
      }
    )
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
    wx.showLoading({
      title: '加载中...',
    })
    http.get(`https://app.vmovier.com/apiv3/index/getIndexPosts/lastid/${this.data.lastId}`).then(
      res => {
        
        wx.hideLoading()
        // console.log(res.data.data.list);
        let arr = res.data.data.list;
        let newArr = this.data.comList;
        let lastid = res.data.data.lastid;
        newArr = newArr.concat(arr);
        // console.log(res.data.data.list);
        // this.getTime();
        // console.log(arr);
        this.setData({
          comList:newArr,
          lastId:lastid
        })
        
      }
    )
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:"分享给"
    };
  },
  onShareTimeline(){
    return { 
      title:"分享到朋友圈"
    };
  }
})